package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishPage;
import com.itheima.service.DishService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Envy
 */
@RestController
@RequestMapping("/dish")
public class DishController {
    @Resource
    DishService dishService;

    /**
     * 菜品分页查询
     */
    @GetMapping("/page")
    public R<DishPage> pageQuery( String name,   Integer page,   Integer pageSize) {
        Integer pageMin = (page - 1) * pageSize;
        return dishService.pageQuery(name, pageMin, pageSize);
    }

    /**
     * 根据id查询菜品信息
     */
    @GetMapping("/{id}")
    public R<Dish> selectInfo(@PathVariable String id) {
        return dishService.selectInfo(id);
    }

    /**
     * 菜品下拉列表
     */
    @GetMapping("/list")
    public R<List<Dish>> pullDown() {
        List<Dish> dishes = dishService.pullDown();
        if (dishes != null) {
            return R.success(dishes);
        } else {
            return R.error("暂无数据");
        }
    }

    /**
     * 新增菜品
     */
    @PostMapping()
    public R<Dish> addDish(@RequestBody Dish dish) {
        return dishService.addDish(dish);
    }

    /**
     * 删除菜品
     */
    @DeleteMapping
    public R<Object> delete(@RequestParam("ids") List<String> ids) {
        return dishService.delete(ids);
    }

    /**
     * 修改菜品
     */
    @PutMapping()
    public R<Dish> update(@RequestBody Dish dish) {
        return dishService.update(dish);
    }

    /**
     * 菜品停售/起售
     */
    @PostMapping("/status/{status}")
    public R<Object> updatestatus(@RequestParam List<String> ids, @PathVariable Integer status) {
        return dishService.updatestatus(ids, status);
    }
}